package me.zhengjie.modules.zhihu.common;

import cn.hutool.http.Header;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.pool.ipproxy.domain.IPInfo;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.util.List;

public class QuesiontUtils {
    public QuesiontUtils() {
    }

    public static String getQuestionVisitNum(String url) {
            String result = HttpRequest.get(url)
                    .header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36")
                    .header(Header.REFERER, "https://www.zhihu.com/topic/19678042/unanswered")
                    .header(Header.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9")
                    .header(Header.CACHE_CONTROL, "no-cache")
                    .header(Header.ACCEPT, "*/*").execute().body();
            String numStr = "";

            numStr = result.substring(result.lastIndexOf("NumberBoard-itemValue") + "NumberBoard-itemValue\" title=\"".length(), result.lastIndexOf("NumberBoard-itemValue") + "NumberBoard-itemValue\" title=\"".length() + 10);
            String num =  numStr.substring(0, numStr.indexOf("\""));
            return num;

    }

//    public static String getQuestionVisitNum(String url, int count, List<String> range)throws Exception {
//// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipBean.getIp(), ipBean.getPort()));
//        //SocketAddress socketAddress = new InetSocketAddress("");
//        //Proxy p = new Proxy(Proxy.Type.HTTP,);
//        if (count >9 || range.size() < count) throw new Exception("所有代理IP被封");
//        IPInfo ipInfo = JSONObject.parseObject( range.get(count) , IPInfo.class ) ;
//        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipInfo.getIp(), Integer.valueOf(ipInfo.getPort())));
//        try {
//            String result = HttpRequest.get(url)
//                    .header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36")
//                    .header(Header.REFERER, "https://www.zhihu.com/topic/19678042/unanswered")
//                    .header(Header.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9")
//                    .header(Header.CACHE_CONTROL, "no-cache").setProxy(proxy)
//                    .header(Header.ACCEPT, "*/*").execute().body();
//            String numStr = "";
//
//            numStr = result.substring(result.lastIndexOf("NumberBoard-itemValue") + "NumberBoard-itemValue\" title=\"".length(), result.lastIndexOf("NumberBoard-itemValue") + "NumberBoard-itemValue\" title=\"".length() + 10);
//            String num =  numStr.substring(0, numStr.indexOf("\""));
//            Integer.parseInt(num);
//            count++;
//            return num;
//        } catch (HttpException e) {
//            getQuestionVisitNum(url,count,range);
//        }
//        return "";
//    }
}
